package com.android.bitmapfun.util;
import java.io.Serializable;

import com.linkrenhe.frame.http.HttpBitmapRequest;
/**
 * 加载图片的实体
 * @author bo
 *
 */
public abstract class LoadImage implements Serializable {
	public static final int MAX_WIDTH = 960;
    public static final int MAX_HEIGHT = 960;
    
    private static final long serialVersionUID = -1553083837863333470L;
    protected int width = MAX_WIDTH;
    protected int height = MAX_HEIGHT;
    protected int outWidth = -1;
    protected int outHeight = -1;
    protected boolean cacheMemoryAvailable = false;
    protected boolean cacheStoregeAvailable = false;

   
    /**
     * 下载是否能找到    */
    private boolean downloadAvailable = true;

    /**
     * 正在下载显示图片的资源Id
     */
    private int loadingImageResId;

    /**
     * 加载图片
     * 
     * @param localPath 本地路径
     * @param maxWidth 最大宽度
     * @param maxHeight 最大高度
     * @param isNeedCacheMemory 是否要缓存到内存  
     *  @param isNeedCacheStorege  是否要存到存储卡 
     */
    public LoadImage(int maxWidth, int maxHeight, boolean cacheMemory, boolean cacheStorege) {
        this.width = maxWidth;
        this.height = maxHeight;
        this.cacheMemoryAvailable = cacheMemory;
        this.cacheStoregeAvailable = cacheStorege;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }
    
    /**
     * 设置输出的尺寸
     * @param outWidth
     * @param outHeight
     */
    public void setOutMeasureSize(int outWidth,int outHeight){
    	this.outWidth = outWidth;
    	this.outHeight = outHeight;
    }

    public int getOutWidth() {
		return outWidth;
	}

	public int getOutHeight() {
		return outHeight;
	}

	public void setHeight(int height) {
        this.height = height;
    }

    public abstract String getSourceCacheKey();

    public abstract boolean isComplete();

    public abstract String getCacheKey();

    public int getLoadingImageResId() {
        return loadingImageResId;
    }

    public boolean isCacheMemoryAvailable() {
        return cacheMemoryAvailable;
    }

    public void setCacheMemoryAvailable(boolean cacheMemoryAvailable) {
        this.cacheMemoryAvailable = cacheMemoryAvailable;
    }

    public boolean isCacheStoregeAvailable() {
        return cacheStoregeAvailable;
    }

    public void setCacheStoregeAvailable(boolean cacheStoregeAvailable) {
        this.cacheStoregeAvailable = cacheStoregeAvailable;
    }

    public boolean isDownloadAvailable() {
        return downloadAvailable;
    }

    public void setDownloadAvailable(boolean downloadAvailable) {
        this.downloadAvailable = downloadAvailable;
    }

    public void setLoadingImageResId(int loadingImageResId) {
        this.loadingImageResId = loadingImageResId;
    }
    
    public static String getStyleNameByWidth(int width) {
        if (width >= 810) {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_810;
        } else if (width > 600) {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_600;
        } else if (width >= 480) {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_480;
        } else if (width >= 360) {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_360;
        } else if (width >= 160) {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_160;
        } else if (width >= 100) {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL;
        } else {
            return HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL;
        }
    }
    
    public static int getWidthByStyleName(String styleName){
    	if(HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_810.equals(styleName)){
    		return 810;
    	}
    	else if(HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_600.equals(styleName)){
    		return 600;
    	}
    	else if(HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_480.equals(styleName)){
    		return 480;
    	}
    	else if(HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_360.equals(styleName)){
    		return 360;
    	}
    	else if(HttpBitmapRequest.STYLE_NAME_IMAGE_SMALL_160.equals(styleName)){
    		return 160;
    	}
    	else {
    		return 100;
    	}
    }
}
